import { defineStore } from 'pinia'
import { ElMessage } from 'element-plus'
import {
  getMomentList,
  getMomentById,
  searchMoment,
  getMomentLabels,
  commentById,
  removeCommentById,
  participateComment,
  searchMomentByLabel
} from '@/service/blog/study'

interface projectState{
  momentList: any[],
  total: number,
  labels: any[]
}

const useStudyStore = defineStore('study', {
  state: ():projectState => ({
    momentList: [],
    total: 0,
    labels: []

  }),
  actions: {
    getMomentListAction(queryInfo: any){
      getMomentList(queryInfo).then(res => {
        // console.log(res)
        this.momentList = res.data
        this.total = res.total
      })
    },
    getMomentByIdAction(momentId: any) {
      return getMomentById(momentId)
    },
    //搜索
    searchMomentAction(content: string) {
      searchMoment(content).then(res => {
        if(res.data.total === 0) {
          this.getMomentListAction({offset: 0 ,size: 10})
          ElMessage({message: '无查询结果', type: 'warning'})
        }else {
          this.momentList = res.data.result
          this.total = res.data.total
          ElMessage({message: res.msg, type: 'success'})
        }
      })
    },
    getMomentLabelsAction(limit: number, offset: number) {
      getMomentLabels(limit, offset).then(res => {
        // console.log(res)
        this.labels = res.data
      })
    },
    commentByIdAction(commentId: number, momentId: any, content: string) {
      commentById(commentId,momentId,content).then(res => {
        // console.log(res)
        ElMessage({message: res.msg, type: 'success'})
      })
    },
    removeCommentByIdAction(commentId: number) {
      removeCommentById(commentId).then(res => {
        // console.log(res)
        if(res.code == 200) {
          ElMessage({message: res.msg, type: 'success'})
        }else {
          ElMessage({message: res.response.data, type: 'error'})
        }
      })
    },
    participateCommentAction(momentId: any, content: string) {
      participateComment(momentId, content).then(res => {
        // console.log(res)
        if(res.code == 200) {
          ElMessage({message: res.msg, type: 'success'})
        }else {
          ElMessage({message: res.response.data, type: 'error'})
        }
      })
    },
    searchMomentByLabelAction(labelName: string) {
      searchMomentByLabel(labelName).then(res => {
        console.log(res)
        this.momentList = res
      })
    }
  }
})

export default useStudyStore
